package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.health.constant.MessageConstant;
import com.itcast.health.constant.QiNiuUtils;
import com.itcast.health.constant.RedisConstant;
import com.itcast.health.pojo.PageResult;
import com.itcast.health.pojo.QueryPageBean;
import com.itcast.health.pojo.Result;
import com.itcast.health.pojo.Setmeal;
import com.itcast.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.Map;
import java.util.UUID;

/**
 * @author Gao
 * @version v1.0
 * @date 2019/11/20 10:35
 * @description
 **/
@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.findPage(queryPageBean);
    }

    /**
     * 新增
     */
    @RequestMapping("add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
            setmealService.add(setmeal,checkgroupIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);


    }

    /**
     * 查询编辑套餐时需要回显的数据
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id){
        Map map=null;
        try {
            map=setmealService.findById(id);
        } catch (Exception e) {
           return new Result(false,"查询失败,请刷新页面");
        }
        return new Result(true,"",map);
    }

    /**
     * 编辑套餐信息
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){

        try {
            setmealService.edit(setmeal,checkgroupIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);

    }

    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            setmealService.delete(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * 文件上传
     * @param imgFile
     * @return
     */
    @RequestMapping("upload")
    public Result upload(MultipartFile imgFile){
        try {
            String originalFileName=imgFile.getOriginalFilename();
            int lastIndexOf = originalFileName.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFileName.substring(lastIndexOf);
            //使用uuid随机产生文件名称,防止文件重名
            String fileName = UUID.randomUUID().toString() + suffix;
            //上传到七牛云
            QiNiuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.PIC_UPLOAD_FAIL);

        }

    }


}